/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaAsignacionGrupos.java
 *
 * Created on Sep 2, 2009, 11:47:42 PM
 */

package ac.view;

import ac.entities.Especie;
import ac.entities.Grupo;
import ac.entities.Tanque;
import ac.entities.Variedad;
import ac.entities.VariedadGrupo;
import ac.logic.LogicEspecie;
import ac.logic.LogicGrupo;
import ac.logic.LogicTanque;
import ac.logic.LogicVariedad;
import ac.logic.LogicVariedadGrupo;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author prometheus
 */
public class VentanaAsignacionGrupos extends javax.swing.JFrame {

    private ArrayList<Grupo> grupos = null;
    private Grupo seleccionado = null;
    private Especie especie = null;
    private ArrayList<Tanque> tanques = null;
    private Variedad variedad = null;

    /** Creates new form VentanaAsignacionGrupos */
    public VentanaAsignacionGrupos() {
        LookAndFeel laf = new LookAndFeel();
        initComponents();
        DefaultListModel modelo = null;
        LogicGrupo lg = new LogicGrupo();
        this.grupos = lg.buscarTodosGrupos();
        modelo = this.setearModelo();
        this.listaGrupos.setModel(modelo);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listaGrupos = new javax.swing.JList();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        campoTanque = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        campoEspecie = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        campoVariedad = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        campoEstimado = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        comboTanques = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        campoCantidad = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Grupos"));

        listaGrupos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listaGruposMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(listaGrupos);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Detalles del Grupo"));

        jLabel1.setText("Tanque:");

        jLabel2.setText("Especie:");

        jLabel3.setText("Variedad:");

        jLabel4.setText("Estimado:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoTanque, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(campoEstimado, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(campoVariedad)
                                .addComponent(campoEspecie, javax.swing.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(campoTanque, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(campoEspecie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(campoVariedad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(campoEstimado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Tanque Destino"));

        jLabel5.setText("Tanques Sugeridos:");

        jLabel6.setText("Cantidad de Individuos:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(comboTanques, 0, 296, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(comboTanques, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(campoCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(32, Short.MAX_VALUE))
        );

        jButton1.setText("Asignar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton2)))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void listaGruposMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listaGruposMouseClicked
        int seleccion = this.listaGrupos.getSelectedIndex();
        this.seleccionado = this.grupos.get(seleccion);
        this.buscarEspecieyVariedad();

        int estimado = this.seleccionado.getEstimado();
        this.campoEstimado.setText(Integer.toString(estimado));

        this.cargarTanquesSugeridos(this.especie);
    }//GEN-LAST:event_listaGruposMouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String mensaje = "No se ha posiso asignar el animal al tanque";
        LogicVariedadGrupo lva = new LogicVariedadGrupo();

        LogicGrupo lg = new LogicGrupo();
        LogicTanque lt = new LogicTanque();

        Tanque t = lt.buscarTanqueActual(this.seleccionado.getId());
        int idGrupo = lg.ultimoIdGrupo() + 1;

        int idGrupoViejo = this.seleccionado.getId();

        int selectedTankOnList = this.comboTanques.getSelectedIndex();
        Tanque selected = this.tanques.get(selectedTankOnList - 1);
        int idTanque = selected.getId();

        int estimado = Integer.parseInt(this.campoEstimado.getText());
        int cantidad = Integer.parseInt(this.campoCantidad.getText());

        if (estimado == cantidad) {
            Date hoy = new Date();
            int idGrupoNuevo = lg.ultimoIdGrupo() + 1;

            Grupo nuevo = new Grupo();
            nuevo.setId(idGrupoNuevo);
            nuevo.setFechaIni(hoy);
            nuevo.setIdTanque(idTanque);
            nuevo.setEstimado(estimado);

            VariedadGrupo vg = new VariedadGrupo(this.variedad.getId(), nuevo.getId());

            this.seleccionado.setFechaFin(hoy);
            String show = lg.actualizarGrupoFF(this.seleccionado);
            System.out.println("show = " + show);
            mensaje = lg.nuevoGrupo(nuevo, vg);
            JOptionPane.showMessageDialog(this, mensaje, "Info", JOptionPane.INFORMATION_MESSAGE);      
        } else if (cantidad < estimado) {
            Date hoy = new Date();
            this.seleccionado.setEstimado(estimado - cantidad);
            int idGrupoNuevo = lg.ultimoIdGrupo() + 1;
            Grupo nuevo = new Grupo();
            nuevo.setId(idGrupoNuevo);
            nuevo.setFechaIni(hoy);
            nuevo.setEstimado(cantidad);
            nuevo.setIdTanque(idTanque);

            VariedadGrupo vg = new VariedadGrupo (this.variedad.getId(), nuevo.getId());

            String show = lg.actualizarGrupoCantidad(this.seleccionado);
            System.out.println(show);

            mensaje = lg.nuevoGrupo(nuevo, vg);

            JOptionPane.showMessageDialog(this, mensaje, "Info", JOptionPane.INFORMATION_MESSAGE);
        } else if (cantidad > estimado) {
            JOptionPane.showMessageDialog(this, "Cantidad Inválida", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VentanaAsignacionGrupos().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField campoCantidad;
    private javax.swing.JTextField campoEspecie;
    private javax.swing.JTextField campoEstimado;
    private javax.swing.JTextField campoTanque;
    private javax.swing.JTextField campoVariedad;
    private javax.swing.JComboBox comboTanques;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList listaGrupos;
    // End of variables declaration//GEN-END:variables

    private DefaultListModel setearModelo() {
        DefaultListModel result = new DefaultListModel();
        for (Grupo g : this.grupos) {
            result.addElement(g.getId());
        }
        return result;
    }

    private void buscarEspecieyVariedad() {
        LogicVariedad lv = new LogicVariedad();
        LogicEspecie le = new LogicEspecie();
        Especie e = null;
        Variedad v = null;
        v = lv.buscarVariedadIdGrupo(this.seleccionado.getId());
        e = le.buscarEspecieId(v.getIdEspecie());
        this.especie = e;
        this.variedad = v;

        this.campoEspecie.setText(e.getNombreLat());
        this.campoVariedad.setText(v.getNombre());
    }

    private void cargarTanquesSugeridos(Especie especie) {
        this.comboTanques.removeAllItems();
        LogicTanque lt = new LogicTanque();
        this.tanques = lt.buscarTanquesEspecie(especie.getIdEspecie());
        Tanque tanqueActual = lt.buscarTanque(this.seleccionado.getIdTanque());
        String infoActual = tanqueActual.getForma() + " " + tanqueActual.getMaterial() +
                " " + tanqueActual.getCapacidad() + " Lts";
        if (tanqueActual.getCuarentena().equalsIgnoreCase("si")) {
            infoActual += " Q";
        }
        this.campoTanque.setText(infoActual);
        this.comboTanques.addItem("Seleccione un Tanque");
        for (Tanque t : this.tanques) {
            String tank = t.getForma() + " " + t.getMaterial() + " " +
                    t.getCapacidad() + " Lts";
            if (t.getCuarentena().equalsIgnoreCase("si")) {
                tank += " " + "Q";
            }
            if ((tanqueActual == null) && (t.getCuarentena().equalsIgnoreCase("si"))) {
                    this.comboTanques.addItem(tank);
            } else if (tanqueActual != null) {
                    this.comboTanques.addItem(tank);
            }
        }
    }

    private boolean validarTiempoCuarentena() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

}
